using RimWorld;
using System;
using System.Collections.Generic;
using Verse;

namespace RimZombieGirl
{
    public class CachedHediff : HediffWithComps, ILinkPawn
    {
        protected static Dictionary<HediffDef, Dictionary<Pawn, CachedHediff>> Cache
        = new Dictionary<HediffDef, Dictionary<Pawn, CachedHediff>>();
        
        private Pawn linkPawn = null;

        public Pawn LinkPawn => linkPawn;

        public static Dictionary<Pawn, CachedHediff> GetAllByHediffs(HediffDef def)
        {
            if (Cache.TryGetValue(def, out var pawnDict))
            {
                return pawnDict;
            }
            return new Dictionary<Pawn, CachedHediff>();
        }

        public static CachedHediff TryGetHediff(Pawn pawn, HediffDef def)
        {
            // 从缓存中获取
            if (Cache.TryGetValue(def, out var pawnDict))
            {
                // 尝试从缓存中获取
                if (pawnDict.TryGetValue(pawn, out var hediff))
                {
                    return hediff;
                }
                else
                {
                    // 尝试从Pawn的HediffSet中直接获取
                    var newHediff = pawn.health.hediffSet.GetFirstHediffOfDef(def) as CachedHediff;
                    if (newHediff != null)
                    {
                        pawnDict[pawn] = newHediff;
                        return newHediff;
                    }
                    else
                    {
                        // 填充null
                        pawnDict[pawn] = null;
                    }
                }
            }
            else
            {
                // 尝试从Pawn的HediffSet中直接获取
                var newHediff = pawn.health.hediffSet.GetFirstHediffOfDef(def) as CachedHediff;
                if (newHediff != null)
                {
                    var innerDict = new Dictionary<Pawn, CachedHediff>();
                    innerDict[pawn] = newHediff;
                    Cache[def] = innerDict;
                    return newHediff;
                }
                else
                {
                    // 填充null
                    Cache[def] = new Dictionary<Pawn, CachedHediff>();
                }
            }
            return null;
        }

        public static void ClearCache()
        {
            Cache.Clear();
        }

        public override void PostAdd(DamageInfo? dinfo)
        {
            if (!Cache.TryGetValue(def, out var innerDict))
            {
                innerDict = new Dictionary<Pawn, CachedHediff>();
                Cache[def] = innerDict;
            }
            innerDict[pawn] = this;
            base.PostAdd(dinfo);
        }

        public override void PostRemoved()
        {
            if (Cache.TryGetValue(def, out var innerDict))
            {
                innerDict.Remove(pawn);
                if (innerDict.Count == 0)
                {
                    Cache.Remove(def);
                }
            }
            base.PostRemoved();
        }

        public override void ExposeData()
        {
            base.ExposeData();
            Scribe_References.Look(ref linkPawn, "linkPawn");
            if (Scribe.mode == LoadSaveMode.PostLoadInit)
            {
                if (!Cache.TryGetValue(def, out var innerDict))
                {
                    innerDict = new Dictionary<Pawn, CachedHediff>();
                    Cache[def] = innerDict;
                }
                innerDict[pawn] = this;
            }
        }

        public void SetLinkPawn(Pawn pawn)
        {
            this.linkPawn = pawn;
        }

    }
}